package data_structure;

import java.util.ArrayList;
import java.util.List;

public class leetcode_0160 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        List<ListNode> lista=new ArrayList<>();
        List<ListNode> listb=new ArrayList<>();
        int sizea=0;
        int sizeb=0;
        ListNode p=headA;
        while(p!=null){
            lista.add(p);
            p=p.next;
            sizea++;
        }
        p=headB;
        while(p!=null){
            listb.add(p);
            p=p.next;
            sizeb++;
        }
        ListNode q=null;
        for(int i=1;sizea-i>=0&&sizeb-i>=0;++i){
            if(lista.get(sizea-i)==listb.get(sizeb-i)) q=lista.get(sizea-i);
            else{
                break;
            }
        }
        return q;
    }
}
